package com.manor.main.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.manor.common.utils.StringUtil;
import com.manor.main.R;
import com.manor.main.bean.MainCardBeans;

import java.util.List;

public class MainCardAdapter extends RecyclerView.Adapter<MainCardAdapter.CardHolder>   {

    private List<MainCardBeans> mCardBeansList;
    private RequestOptions mRequestOptions;
    private CardClickListener clickListener;

    @SuppressLint("CheckResult")
    public MainCardAdapter(List<MainCardBeans> cardBeanList) {
        mCardBeansList = cardBeanList;
        mRequestOptions = new RequestOptions();
        mRequestOptions.placeholder(R.mipmap.main_rv_tu2).error(R.mipmap.main_rv_tu2).diskCacheStrategy(DiskCacheStrategy.NONE);
    }

    @Override
    public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_main_list_item, parent, false);
        return new CardHolder(view);
    }

    @Override
    public void onBindViewHolder(final CardHolder holder, final int position) {
        final MainCardBeans bean = mCardBeansList.get(position);
        Glide.with(holder.itemView).load(bean.getAvatar()).apply(mRequestOptions).into(holder.img);
        holder.name.setText(bean.getName());
        holder.sign.setText(bean.getSignature());
        if ("1".equals(bean.getIsauth())) {
            holder.ivAuth.setVisibility(View.VISIBLE);
            Glide.with(holder.itemView).load(bean.getImmortal()).into(holder.ivAuth);
        } else {
            holder.ivAuth.setVisibility(View.GONE);
        }

        //判断是否达人
        if (StringUtil.isNotEmpty(bean.getLevel_icon())) {
            Glide.with(holder.itemView).load(bean.getLevel_icon()).into(holder.ivDaRen);
            holder.ivDaRen.setVisibility(View.VISIBLE);
        } else {
            holder.ivDaRen.setVisibility(View.GONE);
        }
        //判断是否贵族
        if (StringUtil.isNotEmpty(bean.getGzu_icon())) {
            Glide.with(holder.itemView).load(bean.getGzu_icon()).into(holder.ivGuiZu);
            holder.ivGuiZu.setVisibility(View.VISIBLE);
        } else {
            holder.ivGuiZu.setVisibility(View.GONE);
        }

        //判断是否优质
        if (StringUtil.isNotEmpty(bean.getQuality())) {
            Glide.with(holder.itemView).load(bean.getQuality()).into(holder.ivQua);
            holder.ivQua.setVisibility(View.VISIBLE);
        } else {
            holder.ivQua.setVisibility(View.GONE);
        }

        //判断在线or忙碌  0离线， 2忙碌中 ,3在线
        if ("在线".equals(bean.getLast_online_time())) {
            holder.tvOnlineStatus.setVisibility(View.VISIBLE);
            holder.tvOnlineStatus.setText(bean.getLast_online_time());
            holder.tvOnlineStatus.setBackground(holder.itemView.getResources().getDrawable(R.drawable.bg_main_online3));
        } else if ("接听中".equals(bean.getLast_online_time())) {
            holder.tvOnlineStatus.setVisibility(View.VISIBLE);
            holder.tvOnlineStatus.setText(bean.getLast_online_time());
            holder.tvOnlineStatus.setBackground(holder.itemView.getResources().getDrawable(R.drawable.bg_main_online3));
        } else {
            holder.tvOnlineStatus.setVisibility(View.GONE);
        }

        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) {
                    clickListener.onClicked(position);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return mCardBeansList.size();
    }



    static class CardHolder extends RecyclerView.ViewHolder {
        ImageView img;
        ImageView ivAuth;
        ImageView ivGuiZu;
        ImageView ivDaRen;
        ImageView ivQua;
        TextView sign;
        TextView name;
        TextView tvOnlineStatus;

        public CardHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.card_img);
            name = itemView.findViewById(R.id.tv_name);
            sign = itemView.findViewById(R.id.tv_sign);
            ivAuth = itemView.findViewById(R.id.iv_auth);
            ivGuiZu = itemView.findViewById(R.id.iv_gzuicon);
            ivDaRen = itemView.findViewById(R.id.iv_levelicon);
            ivQua = itemView.findViewById(R.id.iv_qua);
            tvOnlineStatus = itemView.findViewById(R.id.tv_onlineStatus);
        }
    }

    public void seCardtClickListener(CardClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface CardClickListener {
        void onClicked(int pos);
    }

}
